package com.sbbs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sbbs.bean.Bill;
import com.sbbs.bean.BillType;
import com.sbbs.service.BillService;
import com.sbbs.service.BillTypeService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class BillController {

    @Autowired
    private BillService billService;
    @Autowired
    private BillTypeService billTypeService;

    @RequestMapping("/bill/search")
    public String search(ModelMap modelMap, Integer typeId, String fromDate, String toDate){
        List<Bill> bills = billService.searchByMsg(typeId, fromDate, toDate);
        List<BillType> billTypes = billTypeService.selectAll();

        modelMap.addAttribute("fromDate", fromDate);
        modelMap.addAttribute("toDate", toDate);
        modelMap.addAttribute("typeId", typeId);
        modelMap.addAttribute("billTypes", billTypes);
        modelMap.addAttribute("bills", bills);
        return "bill/list";
    }

    @RequestMapping("/bill/toAdd")
    public String toAdd(ModelMap modelMap){
        List<BillType> billTypes = billTypeService.selectAll();
        modelMap.addAttribute("billTypes", billTypes);
        return "/bill/add";
    }

    @RequestMapping("/bill/add")
    public String add(Bill bill){
        billService.insert(bill);
        return "redirect:/bill/list-page";
    }

    @RequestMapping("/bill/toUpdate")
    public String toUpdate(ModelMap modelMap, Integer billId){
        Bill oldBill = billService.getById(billId);
        List<BillType> billTypes = billTypeService.selectAll();

        modelMap.addAttribute("oldBill", oldBill);
        modelMap.addAttribute("billTypes", billTypes);
        return "bill/update";
    }

    @RequestMapping("/bill/update")
    public String update(Bill bill){
        billService.updateById(bill);
        return "redirect:/bill/list-page";
    }

    @RequestMapping("/bill/list-page")
    public String listPage(ModelMap modelMap,
                           Integer typeId,
                           String fromDate,
                           String toDate,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer size){

        PageHelper.startPage(pageNum, size);
        List<Bill> bills = billService.searchByMsg(typeId, fromDate, toDate);
        List<BillType> billTypes = billTypeService.selectAll();
        PageInfo<Bill> pageBills = new PageInfo<>(bills);

        modelMap.addAttribute("pageNum", pageNum);
        modelMap.addAttribute("size", size);
        modelMap.addAttribute("fromDate", fromDate);
        modelMap.addAttribute("toDate", toDate);
        modelMap.addAttribute("typeId", typeId);
        modelMap.addAttribute("billTypes", billTypes);
        modelMap.addAttribute("bills", pageBills.getList());
        modelMap.addAttribute("pageCount", pageBills.getPages());
        return "bill/list-page";
    }

    @RequestMapping("/bill/delete")
    public String delete(Integer typeId,
                         String fromDate,
                         String toDate,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         @RequestParam(defaultValue = "10") Integer size,
                         Integer billId){
        billService.deleteById(billId);

        return "redirect:/bill/list-page";
    }
}
